达梦数据库dm8与PYthon连接访问
发布时间:2024/9/12 14:27:42
达梦数据库
===================
主机名LOCALHOST
账号名
SYSDBA
登录密码
123456789
数据库名DAMENG
实例名DMSERVER
端口5236
=====================
Pycharm默认没有连接达梦的驱动,首先选择给Pycharm添加一个达梦的驱动。
找到自己电脑上达梦的JDBC驱动,因为我电脑上安装了达梦数据库,所有的驱动文件都在他的安装目录里。
通过驱动方式选择创建数据库连接。
SYSDBA
123456789
jdbc:dm://127.0.0.1:5236?schema=PRODUCTION
//====python 配置文件===================================
import dmPython
conn = dmPython.connect(user='SYSDBA', password='123456789',
server='localhost', port=5236)
//======python 调用页面=================================
#!/usr/bin/python
# coding:utf-8
import dmPython
from apps.damengDB import conn
import json
try:
cursor = conn.cursor()
print('python: conn success!')
try:
# 清空表,初始化测试环境
cursor.execute('delete from PRODUCTION.PRODUCT_CATEGORY')
print('python: delete success!')
except (dmPython.Error, Exception) as err:
print(err)
try:
# 插入数据
cursor.execute("insert into PRODUCTION.PRODUCT_CATEGORY(NAME) values('88888'), ('66666')")
print('python: insert success!')
# 删除数据
# cursor.execute("delete from PRODUCTION.PRODUCT_CATEGORY where name='Go'")
# print('python: delete success!')
#
# # 更新数据
# cursor.execute('update PRODUCTION.PRODUCT_CATEGORY set name = \'C++\' where name=\'C\'')
# print('python: update success!')
#
# # 查询数据
# cursor.execute("select name from PRODUCTION.PRODUCT_CATEGORY")
cursor.execute("call mytest_proc(parm_OPType=>101);")
rows = cursor.fetchall()
# 列名
columns = [column[0] for column in cursor.description]
# 将获取的数据转换为字典列表
results = []
for row in rows:
result = dict(zip(columns, row))
results.append(result)
print("字典")
print(results)
for tmp in results:
print(tmp["NAME"])
# 将字典列表转换为JSON格式的字符串
json_data = json.dumps(results)
# 输出JSON数据
print("输出JSON数据")
print(json_data)
# for tmp in json_data:
# print(tmp)
# =======================================
# # 将结果转换为列表的字典
# results = []
# for row in rows:
# results.append(dict(zip(cursor.description, row)))
#
# # 将列表转换为JSON字符串
# json_data = json.dumps(results)
#
# # 输出JSON字符串
# print(json_data)
# == == == == == == == == == == == == == == == == == == == =
# for tmp in res:
# print(tmp)
# print('-----------')
# print(tmp[0])
# print(tmp[1])
# # for c1 in tmp:
# # print(c1)
print('python: select success!')
except (dmPython.Error, Exception) as err:
print(err)
conn.close()
except (dmPython.Error, Exception) as err:
print(err)
//=================================
pip install Flask Flask-SQLAlchemy pymssql